package com.lonty.intelligence.controller;

import com.lonty.intelligence.api.Result;
import com.lonty.intelligence.api.ResultCode;
import com.lonty.intelligence.service.HouseholdService;
import com.lonty.intelligence.vo.HouseholdModifyVo;
import com.lonty.intelligence.vo.HouseholdVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Author 陈璇
 * @Date 2020/4/17 10:53
 * @Version 1.0
 */
@RestController
@RequestMapping("app/household")
@Validated
@Api(tags = "住户接口", produces = "application/json")
public class HouseholdController {

    @Resource
    private HouseholdService householdService;

    @ApiOperation(value = "根据楼栋ID获取房间信息和所有住户信息", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "buildingId", value = "楼栋ID",dataType = "string", paramType = "query", required = true)
    })
    @GetMapping("getHouseholdInfoList")
    public Result getHouseholdInfoList(String buildingId){
        return Result.builder()
                .data(householdService.getHouseholdInfoList(buildingId))
                .build();
    }

    @ApiOperation(value = "保存住户", httpMethod = "POST")
    @PostMapping("saveHousehold")
    public Result saveHousehold(@ModelAttribute HouseholdVo householdVo){
        return householdService.saveHousehold(householdVo);
    }

    @ApiOperation(value = "根据ID修改姓名和手机号", httpMethod = "GET")
    @GetMapping("modifyNameAndTelephone")
    public Result modifyNameAndTelephone(@ModelAttribute HouseholdModifyVo householdModifyVo){
        boolean result = householdService.modifyNameAndTelephone(householdModifyVo);
        if (result){
            return Result.builder().build();
        }
        return Result.builder(ResultCode.OPERATION_ERROR).build();
    }

    @ApiOperation(value = "删除住户", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "住户ID",dataType = "string", paramType = "query", required = true)
    })
    @GetMapping("deleteHousehold")
    public Result deleteHousehold(String id){
        if(householdService.deleteHousehold(id)){
            return Result.builder().build();
        }
        return Result.builder(ResultCode.OPERATION_ERROR).build();
    }

}
